package uc;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.onekit.CALLBACK;
import cn.onekit.android.ACTIVITY;
import co.chuangdian.oflauncher.R;

public class SettingListView extends ExpandableListView {
	Context context;
	public boolean isExpand = false;
	public int currposition;

	public SettingListView(Context context) {
		super(context);
		init(context);
	}

	public SettingListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	BaseExpandableListAdapter adapter;

	void init(final Context context) {
		this.context = context;
		adapter = new SettingAdapter();
		this.setGroupIndicator(null);
		View view = new View(context);
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(72));
		view.setLayoutParams(params);
		this.addFooterView(view);

		this.setAdapter(adapter);
		this.setOnGroupExpandListener(new OnGroupExpandListener() {
			@Override
			public void onGroupExpand(int groupPosition) {
				for (int i = 0; i < adapter.getGroupCount(); i++) {
					if (groupPosition != i) {
						SettingListView.this.collapseGroup(i);
					}
				}
			}
		});
	}

	class SettingAdapter extends BaseExpandableListAdapter {

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return null;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return 0;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
				ViewGroup parent) {
			int[] index = indexes.get(groupPosition);
			if (index[1] <0 || index[0]> 1) {

				return null;

			}
			List<Setting> settings = settingGroups[index[0]].settings;
			Setting setting = settings.get(index[1]);
			convertView = View.inflate(getContext(), R.layout.uc_settinglist_content, null);
			ImageButton editButton = (ImageButton) convertView.findViewById(R.id.editButton);
			ImageButton hideButton = (ImageButton) convertView.findViewById(R.id.hideButton);
			ImageButton deleteButton = (ImageButton) convertView.findViewById(R.id.deleteButton); //
			if (setting.hidden) {
				hideButton.setImageResource(R.drawable.btn_hide_off);
			} else {
				hideButton.setImageResource(R.drawable.btn_hide_on);
			}
			deleteButton.setVisibility(setting.canDelete ? View.VISIBLE : View.GONE);
			editButton.setTag(index);
			hideButton.setTag(index);
			deleteButton.setTag(index);
			if (editCallback != null) {
				editButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						editCallback.run(false, (int[]) v.getTag());
					}
				});
			}
			if (hideCallback != null) {
				hideButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						hideCallback.run(false, (int[]) v.getTag());
					}
				});
			}
			if (deleteCallback != null) {
				deleteButton.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						deleteCallback.run(false, (int[]) v.getTag());
					}
				});
			}
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			int[] index = indexes.get(groupPosition);
			return (index[1] <0? 0 : 1);
		}

		@Override
		public Object getGroup(int groupPosition) {
			return null;
		}

		@Override
		public int getGroupCount() {
			return indexes.size() ;
		}

		@Override
		public long getGroupId(int groupPosition) {
			return 0;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			int[] index = indexes.get(groupPosition);
			SettingGroup settingGroup = settingGroups[index[0]];
			if (index[1] ==-2) {
				convertView= new View(context);
				convertView.setBackgroundResource(R.drawable.bg_low_down);
				LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, ACTIVITY.dp2px(10));
				convertView.setLayoutParams(params);
			}else if (index[1] == -1) {
				convertView = View.inflate(getContext(), R.layout.uc_settinglist_tag, null);
				TextView titleTextView = (TextView) convertView.findViewById(R.id.titleTextView);
				convertView.setBackgroundResource(R.drawable.bg_low_up);
				titleTextView.setText(settingGroup.title);

			} else {
				convertView = View.inflate(getContext(), R.layout.uc_settinglist_row, null);
				/*if (groupPosition == settingGroups[0].settings.size() || groupPosition == indexes.size() - 1) {
					convertView.setBackgroundResource(R.drawable.bg_low_down);
				} else {*/
					convertView.setBackgroundResource(R.drawable.bg_low);
			//	}

				List<Setting> settings = settingGroup.settings;
				Setting setting = settings.get(index[1]);
				TextView titleTextView = (TextView) convertView.findViewById(R.id.titleTextView);
				ImageView iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView); //
				titleTextView.setText(setting.title);
				iconImageView.setImageDrawable(setting.icon);

			}

			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			int[] index = indexes.get(groupPosition);
			return (index[1] != -1);
		}

	}

	SettingGroup[] settingGroups = new SettingGroup[] {};
	List<int[]> indexes = new ArrayList<int[]>();
	CALLBACK<int[]> editCallback, hideCallback, deleteCallback;
	// private SettingAdapter adapter;

	public void setData(SettingGroup[] settingGroups, CALLBACK<int[]> editCallback, CALLBACK<int[]> hideCallback,
			CALLBACK<int[]> deleteCallback) {
		this.settingGroups = settingGroups;
		this.editCallback = editCallback;
		this.hideCallback = hideCallback;
		this.deleteCallback = deleteCallback;
		indexes = new ArrayList<int[]>();
		for (int s = 0; s < settingGroups.length; s++) {
			List<Setting> settings = settingGroups[s].settings;
			indexes.add(new int[] { s, -1 });
			for (int i = 0; i < settings.size(); i++) {
				indexes.add(new int[] { s, i });
			}
			indexes.add(new int[] { s, -2 });
		}
		adapter.notifyDataSetChanged();
	}
}
